package com.lovo.web.controller;

import com.alibaba.fastjson.JSON;
import com.lovo.commom.util.MD5Encryptor;
import com.lovo.commom.vo.ResultVO;
import com.lovo.web.bean.UserBean;
import com.lovo.web.mapper.IUserMapper;
import com.lovo.web.service.IUserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    IUserMapper userMapper;

    @GetMapping("login")
    public ResultVO login(String userName, String pwd) throws NoSuchAlgorithmException {
        UserBean user = userService.login(userName, MD5Encryptor.encrypt(pwd));
        ResultVO resultVO = new ResultVO();
        if (user != null) {
            //把对象转换成json
            String json = JSON.toJSONString(user);
//            //生成Token
//            String token = CreateToken.createToken(json);
            resultVO.setTag(0);
            resultVO.setJson(json);
        } else {
            resultVO.setTag(1);
            resultVO.setJson("");
        }
        return resultVO;
    }


    @GetMapping("fastRegister")
    public ResultVO fastRegister(UserBean userBean) throws NoSuchAlgorithmException {
        ResultVO resultVO = new ResultVO();
        boolean b = userService.fastRegister(userBean);
        if (b) {
            resultVO.setJson("注册成功");
            resultVO.setTag(0);
        } else {
            resultVO.setJson("用户名重复");
            resultVO.setTag(1);
        }
        return resultVO;
    }

    @PostMapping("replenishUserInfo")
    public ResultVO replenishUserInfo(UserBean userBean) throws NoSuchAlgorithmException {
        ResultVO resultVO = new ResultVO();
        boolean b = userService.replenishUserInfo(userBean);
        if (b) {
            resultVO.setJson("注册成功");
            resultVO.setTag(0);
        } else {
            resultVO.setJson("用户名重复");
            resultVO.setTag(1);
        }
        return resultVO;
    }


    @PostMapping("replenishInfo")
    public ResultVO replenishInfo(UserBean userBean) throws NoSuchAlgorithmException {
        ResultVO resultVO = new ResultVO();
        userService.replenishInfo(userBean);
        resultVO.setJson("认证申请成功");
        resultVO.setTag(0);
        return resultVO;
    }

    @RequestMapping("findByUserName/{userName}")
    public UserBean findByUserName(@PathVariable("userName") String UserName) {
        return userService.findByUserName(UserName);
    }

    @RequestMapping("updateUserAuditState")
    public void updateUserAuditState(@RequestParam("userName") String userName, @RequestParam("isPass") Integer isPass, @RequestParam("auditState") Integer auditState,
                                     @RequestParam("content") String content, @RequestParam("adminName") String adminName, @RequestParam("updateDate") String updateDate) {
        userService.updateUserAuditState(userName, isPass, auditState, content, adminName, LocalDate.parse(updateDate));
    }

    @RequestMapping("updateUserFreezeStatus/{userName}/{frozenState}/{cause}")
    public void updateUserFreezeStatus(@PathVariable("userName") String userName, @PathVariable("frozenState") Integer frozenState, @PathVariable("cause") String cause) {
        userService.updateUserFreezeStatus(userName, frozenState, cause);
    }

    @GetMapping("addMoney/{userName}/{money}")
    public boolean addMoney(@PathVariable("userName") String userName, @PathVariable("money") float money) {
        userService.addMoney(userName, money);
        return true;
    }

}
